/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * EditProfile.java
 *
 * Created on Aug 31, 2011, 3:04:56 PM
 */

package view;

import controller.AccountsController;
import controller.PropertiesUtility;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.channels.FileChannel;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.ImageIcon;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import model.UserProfile;

/**
 *
 * @author c0910m1479
 */
public class EditProfile extends javax.swing.JDialog {

    /** Creates new form EditProfile */
    public EditProfile(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        loadValues();
        this.setLocationRelativeTo(null);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jbSave = new javax.swing.JButton();
        address = new javax.swing.JLabel();
        jtfPhone = new javax.swing.JTextField();
        phone = new javax.swing.JLabel();
        jbReset = new javax.swing.JButton();
        jtfPicture = new javax.swing.JTextField();
        jbCancel = new javax.swing.JButton();
        jtfAddress = new javax.swing.JTextField();
        jtfPassword = new javax.swing.JTextField();
        jbBrowse = new javax.swing.JButton();
        picture = new javax.swing.JLabel();
        password = new javax.swing.JLabel();
        caption = new javax.swing.JLabel();
        profilePic = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jbSave.setText("Save");
        jbSave.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbSaveActionPerformed(evt);
            }
        });

        address.setFont(new java.awt.Font("Courier New", 0, 14));
        address.setText("Address:");

        phone.setFont(new java.awt.Font("Courier New", 0, 14));
        phone.setText("Phone Number:");

        jbReset.setText("Reset");
        jbReset.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbResetActionPerformed(evt);
            }
        });

        jbCancel.setText("Cancel");
        jbCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbCancelActionPerformed(evt);
            }
        });

        jbBrowse.setText("Browse");
        jbBrowse.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbBrowseActionPerformed(evt);
            }
        });

        picture.setFont(new java.awt.Font("Courier New", 0, 14));
        picture.setText("Picture:");

        password.setFont(new java.awt.Font("Courier New", 0, 14));
        password.setText("Password:");

        caption.setFont(new java.awt.Font("Courier New", 1, 18));
        caption.setText("PROFILE INFORMATION");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(address)
                    .addComponent(caption)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel1Layout.createSequentialGroup()
                                .addComponent(jbReset)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 142, Short.MAX_VALUE)
                                .addComponent(jbSave)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(jbCancel))
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel1Layout.createSequentialGroup()
                                .addComponent(phone)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jtfPhone, javax.swing.GroupLayout.DEFAULT_SIZE, 227, Short.MAX_VALUE))
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel1Layout.createSequentialGroup()
                                .addComponent(password)
                                .addGap(36, 36, 36)
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jtfPicture, javax.swing.GroupLayout.DEFAULT_SIZE, 227, Short.MAX_VALUE)
                                    .addComponent(jtfPassword, javax.swing.GroupLayout.DEFAULT_SIZE, 227, Short.MAX_VALUE)
                                    .addComponent(jtfAddress, javax.swing.GroupLayout.DEFAULT_SIZE, 227, Short.MAX_VALUE)))
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel1Layout.createSequentialGroup()
                                .addComponent(picture)
                                .addGap(271, 271, 271)))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jbBrowse)))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(7, 7, 7)
                .addComponent(caption)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(password)
                    .addComponent(jtfPassword, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(picture)
                    .addComponent(jtfPicture, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jbBrowse))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(address)
                    .addComponent(jtfAddress, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(phone)
                    .addComponent(jtfPhone, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jbReset)
                    .addComponent(jbCancel)
                    .addComponent(jbSave))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(profilePic)
                .addContainerGap(203, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(47, 47, 47)
                        .addComponent(profilePic)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void jbResetActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbResetActionPerformed
    jtfPassword.setText(null);
    jtfPicture.setText(null);
    jtfAddress.setText(null);
    jtfPhone.setText(null);
}//GEN-LAST:event_jbResetActionPerformed

private void jbCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbCancelActionPerformed
    this.dispose();
}//GEN-LAST:event_jbCancelActionPerformed

private void jbBrowseActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbBrowseActionPerformed
    JFileChooser fileChooser = new JFileChooser();
    if (fileChooser.showDialog(this, "Select") == JFileChooser.APPROVE_OPTION) {
        
        File sourceImage = new File(fileChooser.getSelectedFile().getPath());
        File destDir = new File(System.getProperty("user.dir") + "/images/accounts/");
        File destImage = new File(System.getProperty("user.dir") + "/images/accounts/" + fileChooser.getSelectedFile().getName());
        
        if (!sourceImage.getPath().equals(destImage.getPath())) {
            try {
                if (!destDir.exists()) destDir.mkdirs();
                
                FileChannel inChannel = new FileInputStream(sourceImage).getChannel();
                FileChannel outChannel = new FileOutputStream(destImage).getChannel();
                
                try {
                    if(!(inChannel.size() == inChannel.transferTo(0, inChannel.size(), outChannel))) {
                        JOptionPane.showMessageDialog(fileChooser, "Error Copying File", "Error", JOptionPane.ERROR_MESSAGE);
                    }
                } catch (IOException ex) {
                    Logger.getLogger(EditProfile.class.getName()).log(Level.SEVERE, null, ex);
                } finally {
                    if (inChannel != null) inChannel.close();
                    if (outChannel != null) outChannel.close();
                }
            } catch (IOException ex) {
                Logger.getLogger(EditProfile.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        
        jtfPicture.setText("images/accounts/" + destImage.getName());
        profilePic.setIcon(new ImageIcon(destImage.getPath()));
    }
}//GEN-LAST:event_jbBrowseActionPerformed

private void jbSaveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbSaveActionPerformed
    if(isComponentValid()) {
        
    }
}//GEN-LAST:event_jbSaveActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel address;
    private javax.swing.JLabel caption;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JButton jbBrowse;
    private javax.swing.JButton jbCancel;
    private javax.swing.JButton jbReset;
    private javax.swing.JButton jbSave;
    private javax.swing.JTextField jtfAddress;
    private javax.swing.JTextField jtfPassword;
    private javax.swing.JTextField jtfPhone;
    private javax.swing.JTextField jtfPicture;
    private javax.swing.JLabel password;
    private javax.swing.JLabel phone;
    private javax.swing.JLabel picture;
    private javax.swing.JLabel profilePic;
    // End of variables declaration//GEN-END:variables

    private void loadValues() {
        String[] user = PropertiesUtility.getUserProperties();
        UserProfile profile = AccountsController.getUserProfile(user[0]);
        jtfPassword.setText(profile.getPassword());
        jtfPicture.setText(profile.getPicture());
        jtfAddress.setText(profile.getAddress());
        jtfPhone.setText(profile.getPhone());
        profilePic.setIcon(new ImageIcon(profile.getPicture()));
    }

    private boolean isComponentValid() {
        String pass = jtfPassword.getText();
        String pic = jtfPicture.getText();
        String add = jtfAddress.getText();
        String num = jtfPhone.getText();
        if (pass == null||pass.isEmpty()) {
            JOptionPane.showMessageDialog(this, "Enter your password", "Error", JOptionPane.ERROR_MESSAGE);
            return false;
        } else if (pic == null||pic.isEmpty()) {
            JOptionPane.showMessageDialog(this, "Enter your password", "Error", JOptionPane.ERROR_MESSAGE);
            return false;
        } else if (add == null||add.isEmpty()) {
            JOptionPane.showMessageDialog(this, "Enter your password", "Error", JOptionPane.ERROR_MESSAGE);
            return false;
        } else if (num == null||num.isEmpty()) {
            JOptionPane.showMessageDialog(this, "Enter your password", "Error", JOptionPane.ERROR_MESSAGE);
            return false;
        }
        return true;
    }

}